<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 设备列表 -->
      <el-col :span="8">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>在线设备</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="refreshDeviceList">刷新</el-button>
          </div>
          <div v-for="device in deviceList" :key="device.deviceId" class="device-item" @click="selectDevice(device)">
            <div :class="['device-card', { active: currentDevice && currentDevice.deviceId === device.deviceId }]">
              <div class="device-info">
                <div class="device-name">{{ device.brand }} {{ device.model }}</div>
                <div class="device-detail">
                  <span>ID: {{ device.deviceId }}</span>
                  <span>Android {{ device.release }}</span>
                </div>
                <div class="device-status">
                  <el-tag size="mini" type="success">在线</el-tag>
                  <span class="battery-info">
                    <i :class="getBatteryIcon(device.battery)"></i>
                    {{ device.battery }}%
                    <i v-if="device.isCharging" class="el-icon-loading"></i>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 设备控制面板 -->
      <el-col :span="16">
        <el-card class="box-card" v-if="currentDevice">
          <div slot="header" class="clearfix">
            <span>设备控制 - {{ currentDevice.brand }} {{ currentDevice.model }}</span>
          </div>
          
          <!-- 脚本控制 -->
          <el-form :model="scriptForm" label-width="80px">
            <el-form-item label="脚本名称">
              <el-input v-model="scriptForm.name" placeholder="请输入脚本名称"></el-input>
            </el-form-item>
            <el-form-item label="脚本代码">
              <el-input type="textarea" v-model="scriptForm.code" :rows="10" placeholder="请输入脚本代码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleRunScript">运行脚本</el-button>
              <el-button type="danger" @click="handleStopScript">停止脚本</el-button>
            </el-form-item>
          </el-form>

          <!-- 截图控制 -->
          <div class="screenshot-container">
            <div class="screenshot-header">
              <span>设备截图</span>
              <el-button type="primary" size="mini" @click="handleGetScreenshot">获取截图</el-button>
            </div>
            <div class="screenshot-content" v-if="screenshotData">
              <img :src="'data:image/png;base64,' + screenshotData" alt="设备截图" />
            </div>
          </div>
        </el-card>
        <el-empty v-else description="请选择设备"></el-empty>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getOnlineDevices, getDeviceInfo, runScript, stopScript, getScreenshot } from "@/api/iot/control";

export default {
  name: "DeviceControl",
  data() {
    return {
      // 设备列表
      deviceList: [],
      // 当前选中的设备
      currentDevice: null,
      // 脚本表单
      scriptForm: {
        name: '',
        code: ''
      },
      // 截图数据
      screenshotData: null,
      // 刷新定时器
      refreshTimer: null
    };
  },
  created() {
    this.getList();
    // 定时刷新设备列表
    this.refreshTimer = setInterval(() => {
      this.getList();
    }, 30000);
  },
  beforeDestroy() {
    if (this.refreshTimer) {
      clearInterval(this.refreshTimer);
    }
  },
  methods: {
    /** 获取设备列表 */
    getList() {
      getOnlineDevices().then(response => {
        this.deviceList = response.data;
      });
    },
    /** 刷新设备列表 */
    refreshDeviceList() {
      this.getList();
    },
    /** 选择设备 */
    selectDevice(device) {
      this.currentDevice = device;
      this.screenshotData = null;
    },
    /** 运行脚本 */
    handleRunScript() {
      if (!this.scriptForm.name || !this.scriptForm.code) {
        this.$message.warning("请填写完整的脚本信息");
        return;
      }
      runScript(this.currentDevice.deviceId, this.scriptForm).then(() => {
        this.$message.success("脚本已发送到设备");
      });
    },
    /** 停止脚本 */
    handleStopScript() {
      this.$confirm('确认停止当前设备的所有脚本?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        stopScript(this.currentDevice.deviceId).then(() => {
          this.$message.success("停止指令已发送到设备");
        });
      });
    },
    /** 获取截图 */
    handleGetScreenshot() {
      getScreenshot(this.currentDevice.deviceId).then(response => {
        if (response.data) {
          this.screenshotData = response.data;
        }
      });
    },
    /** 获取电池图标 */
    getBatteryIcon(battery) {
      if (battery >= 80) return 'el-icon-battery-full';
      if (battery >= 60) return 'el-icon-battery-three-quarters';
      if (battery >= 40) return 'el-icon-battery-half';
      if (battery >= 20) return 'el-icon-battery-quarter';
      return 'el-icon-battery-empty';
    }
  }
};
</script>

<style scoped>
.device-item {
  margin-bottom: 10px;
  cursor: pointer;
}

.device-card {
  padding: 10px;
  border: 1px solid #EBEEF5;
  border-radius: 4px;
  transition: all 0.3s;
}

.device-card:hover {
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}

.device-card.active {
  border-color: #409EFF;
  background-color: #ecf5ff;
}

.device-info {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.device-name {
  font-weight: bold;
}

.device-detail {
  font-size: 12px;
  color: #909399;
  display: flex;
  justify-content: space-between;
}

.device-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.battery-info {
  font-size: 12px;
  color: #909399;
}

.screenshot-container {
  margin-top: 20px;
}

.screenshot-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.screenshot-content {
  text-align: center;
}

.screenshot-content img {
  max-width: 100%;
  height: auto;
}
</style> 